// pages/my-pages/edit-item/index.js
const httpUtils = require('../../../utils/httpUtils')
const ui = require('../../../utils/ui')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    item: {},
    id: -1,
    showTextAndTitleWithInput: false,
    baseItemList: [],
    baseItemSize: 0
  },
  //改变基础项目选中数量
  setSelectSize: function () {
    let that = this;
    let selectList = that.data.baseItemList;
    let oldSelect = that.data.item.baseItemList;
    if (undefined != selectList && selectList.length != 0) {
      that.setData({
        baseItemSize: selectList.length,
        'item.baseItemList': selectList
      })
    } else if (undefined != oldSelect && oldSelect.length != 0) {
      that.setData({
        baseItemList: oldSelect,
        baseItemSize: oldSelect.length,
      })
    }
  },
  //点击删除弹窗的确定
  confirmChangeMember: function (e) {
    let that = this;
    let id = that.data.id;
    let obj = {
      method: 'GET',
      showLoading: false,
      url: 'item/delItem?id=' + id
    }
    httpUtils.request(obj).then(res => {
      console.log(res.data.message)
      if (res.data.retCode == 0) {
        wx.navigateBack({
          detail: 1
        })
      } else {
        ui.showToast(res.data.message)
      }
    }).catch(err => {
      ui.showToast('请求失败')
    });
  },
  //点击删除弹窗的取消
  closeDialog: function () {
    this.setData({
      ['showTextAndTitleWithInput']: false
    });
  },
  //点击了删除按钮
  onDelete: function (e) {
    let item = e.currentTarget.dataset.entity;
    this.setData({
      ['showTextAndTitleWithInput']: true,
      item: item
    })
  },
  //点击保存
  confirmSaveButton: function (e) {
    let that = this;
    let obj;
    if (-1 == that.data.id) {
      obj = {
        method: 'POST',
        showLoading: true,
        url: 'item/addItem',
        data: that.data.item
      }
    } else {
      obj = {
        method: 'POST',
        showLoading: true,
        url: 'item/updateItem',
        data: that.data.item
      }
    }
    httpUtils.request(obj).then(res => {
      if (res.data.retCode == 0) {
        ui.showToast('保存成功')
        wx.navigateBack({
          detail: 1
        })
      } else {
        ui.showToast(res.data.message)
      }
    }).catch(err => {
      ui.showToast('请求失败')
    });
  },
  //消费名称信息变更
  inputItemName: function (e) {
    this.setData({
      'item.itemName': e.detail.value
    })
  },
  //顺序编号变更
  inputSortNumber: function (e) {
    let sortNumber = e.detail.value;
    sortNumber = sortNumber.replace(/\D/g, '')
    this.setData({
      'item.sortNumber': sortNumber
    })
  },
  //默认价格变更
  inputDefaultPrice: function (e) {
    let defaultPrice = e.detail.value;
    defaultPrice = defaultPrice.replace(/\D/g, '')
    this.setData({
      'item.defaultPrice': defaultPrice
    })
  },
  //查询消费单信息
  getItemFunc: function () {
    let that = this;
    let id = that.data.id;
    if (-1 == id || null == id) {
      that.setData({
        item: {}
      })
    } else {
      //获取消费单信息
      let obj = {
        method: 'GET',
        showLoading: false,
        url: 'item/getItemById?id=' + id
      }
      httpUtils.request(obj).then(res => {
        console.log(res.data.message)
        if (res.data.retCode == 0) {
          this.setData({
            item: res.data.data
          })
          this.setSelectSize();
        } else {
          ui.showToast(res.data.message)
        }
      }).catch(err => {
        ui.showToast('请求失败')
      });
    }
  },
  //点击了基础项目选择
  checkBaseItemButton: function () {
    console.log('点击了基础项目选择')
    wx.navigateTo({
      url: '/pages/my-pages/select-base-item/index',
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      id: options.id,
      item: {}
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getItemFunc();
    this.setSelectSize();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})